/*
 * Copyright 2007-2009 Thomas Krause (thomas@berlin-reloaded.de)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gidoo.cms;

import java.io.File;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.gidoo.cms.jcr.BackupManager;
import org.gidoo.cms.login.CommonLoginPage;
import org.gidoo.cms.login.GidooCMSSession;

/**
 * A WicketTester for gidooCMS
 * @author thomas
 */
public class GidooCMSTester extends WicketTester
{

  private static GidooCMSTester singletonTester;

  public GidooCMSTester()
  {
    super(new GidooCMS() {

      @Override
      public DefaultConfiguration createNewConfiguration()
      {
        return new DefaultConfiguration();
      }
    },
      new File("./src/test/webapp/").getAbsolutePath());

    GidooCMSSession session = (GidooCMSSession) getWicketSession();
    // login if not first runtime
    if(!session.isFirstRuntime())
    {
      session.signIn("admin", "rfvtgb");
    }
    BackupManager.importCMSFromFile(new File("./src/test/test_cms.zip"), session);
    session.signOut();

  }

  /**
   * Get a singleton instance of GidooCMSTester.
   * @return
   */
  public static GidooCMSTester getSingletonTester()
  {
    if(singletonTester == null)
    {
      singletonTester = new GidooCMSTester();
    }
    return singletonTester;
  }

  /**
   * Will fill out a login-page with the example data and will submit the form.
   * @param tester
   */
  public static void performLogin(WicketTester tester)
  {
    tester.assertRenderedPage(CommonLoginPage.class);
    FormTester formTester = tester.newFormTester("signInPanel:signInForm");
    formTester.setValue("username", "admin");
    formTester.setValue("password", "rfvtgb");
    formTester.submit();
  }


}
